package com.bea.ftd.dashboard.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bea.ftd.common.ValidationException;
import com.bea.ftd.common.entity.ApiDefine;
import com.bea.ftd.dashboard.domain.AjaxResponse;
import com.bea.ftd.dashboard.enums.ResponseStatus;
import com.bea.ftd.dashboard.feign.ConfigurationFeign;
import com.bea.ftd.dashboard.service.IApiDefineService;

@RestController
@RequestMapping("/apiDefine")
public class ApiDefineController {
	private Logger LOGGER = LoggerFactory.getLogger(ApiDefineController.class);

	@Autowired
	private IApiDefineService apiDefineServiceImpl;
	@Autowired
	private ConfigurationFeign configurationFeign;

	@RequestMapping("/listAll")
	public List<ApiDefine> listAllApiDefines() {
		return apiDefineServiceImpl.getAllApiDefines();
	}

	@RequestMapping("/getAllApiDefineIds")
	public List<String> getAllApiDefineIds() {
		return apiDefineServiceImpl.getAllApiDefineIds();
	}
	
	@RequestMapping("/saveOrUpdate")
	public AjaxResponse addApiDefine(ApiDefine apiDefine) {
		LOGGER.info("Save or update apiDefine - " + apiDefine.toString());
		try {
			apiDefineServiceImpl.addApiDefine(apiDefine);
			return new AjaxResponse(ResponseStatus.SUCCESS);
		} catch (ValidationException e) {
			LOGGER.error("输入参数错误：" + e.getErrorMessage());
			return new AjaxResponse(ResponseStatus.FAIL, e.getErrorMessage());
		} 
		catch (Exception e) {
			LOGGER.error("Error in saving apiDefine to DB. " + e.getMessage());
			return new AjaxResponse(ResponseStatus.FAIL, e.getMessage());
		}

	}
	
	@RequestMapping("/refreshConfiguration")
	public AjaxResponse refreshConfiguration() {
		return configurationFeign.refreshConfiguration();
	}

}
